package com.lc.sell.service.serviceImpl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lc.sell.dto.CartDTO;
import com.lc.sell.emnus.ResultEnum;
import com.lc.sell.entity.Product;
import com.lc.sell.exception.SellException;
import com.lc.sell.mapper.ProductMapper;
import com.lc.sell.service.ProductService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lc.sell.util.TimeUtils;
import com.lc.sell.vo.ResultVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;


import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 龙晨
 * @since 2022-02-01
 */
@Service
public class ProductServiceImpl extends ServiceImpl<ProductMapper, Product> implements ProductService {

    @Autowired
    private ProductMapper productMapper;

    @Override
    public ResultVo insertProduct(Product product) {
        product.setCreateTime(TimeUtils.getNowDate());
        int i = productMapper.insert(product);
        if (i>0){
            return ResultVo.ok("添加成功");
        }
        return ResultVo.fail("添加失败");
    }

    @Override
    public ResultVo findAllProductByMid(Integer mid,Long current, Long size) {
        Page<Product> all = productMapper.selectPage(new Page<>(current, size), new QueryWrapper<Product>().eq("mid", mid));
        return ResultVo.ok(0,"成功",all);
    }

    @Override
    public ResultVo updateProduct(Product product) {
        int i = productMapper.updateById(product);
        if (i>0){
            return ResultVo.ok("修改成功");
        }
        return ResultVo.fail("修改失败");
    }

    @Override
    public ResultVo deleteProduct(Integer pid) {
        int i = productMapper.deleteById(pid);
        if (i>0){
            return ResultVo.ok("删除成功");
        }
        return ResultVo.fail("删除失败");
    }

    @Override
    public ResultVo findByProduct(Product product) {
        List<Product> list = productMapper.selectList(new QueryWrapper<Product>()
                .like(StringUtils.isNotBlank(product.getName()),"name", product.getName())
                .like(product.getPrice() != null,"price",product.getPrice())
                .like(StringUtils.isNotBlank(product.getContent()),"content",product.getContent())
                .like(product.getIsSell() != null,"is_sell",product.getIsSell())
                .like(product.getStock() != null ,"stock",product.getStock())
                .like(product.getMid() != null ,"mid",product.getMid())
        );
        return ResultVo.ok("查询成功",list);
    }

    @Override
    @Transactional
    public ResultVo updateProductIsSell(Integer id) {
        Product product = productMapper.selectById(id);
        if (product.getIsSell()==0){
           product.setIsSell(1);
        }else {
            product.setIsSell(0);
        }
        productMapper.updateById(product);
        return ResultVo.ok("切换成功");
    }

    @Override
    @Transactional
    public ResultVo updateStock(List<CartDTO> cartDTOList) {
        for (CartDTO cartDTO : cartDTOList) {
            Product product = productMapper.selectById(cartDTO.getProductId());
            if(product==null){
                throw new SellException(ResultEnum.PRODUCT_NOT_EXIST);
            }
            Integer result= product.getStock()-cartDTO.getProductQuantity();
            if (result<0){
                throw new SellException(ResultEnum.PRODUCT_STOCK_ERROR);
            }
            product.setStock(result);
            productMapper.updateById(product);
        }
        return ResultVo.ok("减库存成功");
    }

    @Override
    @Transactional
    public ResultVo addSold(List<CartDTO> cartDTOs) {
        for (CartDTO cartDTO : cartDTOs) {
            Product product = productMapper.selectById(cartDTO.getProductId());
            if(product==null){
                throw new SellException(ResultEnum.PRODUCT_NOT_EXIST);
            }
            Integer result= Integer.valueOf(product.getSold())+cartDTO.getProductQuantity();
            product.setSold(String.valueOf(result));
            productMapper.updateById(product);
        }
        return ResultVo.ok("加销量成功");
    }
}
